<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/11
 * Time: 17:43
 */

namespace app\shop\model;


use think\Model;

class ConfigsModel extends Model
{
    protected $table = 'dp_config';
    public static function getConfigItem($name)
    {
        $data = self::where('name', $name)->find();
        $data['long_text'] = json_decode($data['long_text'],true);
        return $data['long_text'];
    }

    public static function saveConfigItem($name, $data, $type='')
    {
        $d['name'] = $name;
        $d['type'] = $type;
        if(is_array($data)){
            $d['long_text'] = json_encode($data);
        }else{
            $d['value'] = $data;
        }
        if(self::nameIsExist($name)){
            return self::where('name',$name)->update($d);
        }else{
            return self::insert($d);
        }

    }

    public static function delConfig($name)
    {
        return self::where('name',$name)->delete();
    }

    public static function nameIsExist($name)
    {
        return self::where('name', $name)->count();
    }
}